package work.xkong.controller;


import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import work.xkong.common.ResponseRest;
import work.xkong.service.IStudentCourseService;
import work.xkong.utils.TokenUtils;
import work.xkong.vo.inputvo.StudentCourseInputVo;

import javax.servlet.http.HttpServletRequest;
import javax.sound.sampled.Clip;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author NuanYang
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/studentCourse")
@Slf4j
public class StudentCourseController {
    @Autowired
    private IStudentCourseService studentCourseService;

    @PostMapping("/saveCourse")
    public ResponseRest saveCourse(HttpServletRequest request, StudentCourseInputVo sci) {
        log.info("前端接收到的参数====>"+sci);

        String jwt = request.getHeader("Authorization");

        String studentId = (String) TokenUtils.getParseToken(jwt).get(Claims.ID);

        sci.setStudentId(Integer.parseInt(studentId));

        return studentCourseService.saveCourse(sci);
    }

    @DeleteMapping("/removeCourse/{courseId}")
    public ResponseRest removeCourse(HttpServletRequest request,@PathVariable Integer courseId){

        StudentCourseInputVo sci = new StudentCourseInputVo();

        sci.setCourseId(courseId);

        String jwt = request.getHeader("Authorization");

        String studentId = (String) TokenUtils.getParseToken(jwt).get(Claims.ID);

        sci.setStudentId(Integer.parseInt(studentId));

        return studentCourseService.removeCourse(sci);
    }

}
